package com.houger.ddd.seckill.repository;

import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author Houger.
 * @date 2024年05月20日 17:30.
 */
@Repository
public interface ActivityRewardItemRepository extends JpaRepository<ActivityRewardItem,Long>, JpaSpecificationExecutor<ActivityRewardItem> {
    Optional<List<ActivityRewardItem>> findByTemplateId(Long templateId);

    void deleteByTemplateId(Long templateId);


    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update activity_reward_item set inv_used =  ?2 where id = ?1",nativeQuery=true)
    int updateInvUsedById(Long itemId, long usedCount);
}
